package com.wash.shoes.service;

import com.alibaba.fastjson.JSON;
import com.wash.shoes.domain.vo.HaoHanUploadBarCodeVO;
import com.wash.shoes.domain.washer.WashReturn;
import com.wash.shoes.domain.washer.WasherDto;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @description: 11
 * @author: wanxy
 * @date: 2025-02-10 22:35
 **/
@Component
public class HttpClientService {

    private static final String batchUrl = "https://pdapi.hfshanku.com/YiWu/ClothesLoadAll?qudaoid=76";

    private static final String singleUrl = "https://pdapi.hfshanku.com/YiWu/ClothesLoad?qudaoid=76";

    private static final String queryTiaoMaUrl = "https://pdapi.hfshanku.com/YiWu/GetTiaoMa";

    String appCode = "46f89d8243a7078afaf2dc029f338058";


    public WashReturn sendPostTiaoMa(List<WasherDto> washerDtoList) {

        RestTemplate restTemplate = new RestTemplate();


        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("Authorization", "appCode " + appCode);

        HttpEntity<List<WasherDto>> entity = new HttpEntity<>(washerDtoList, headers);

        ResponseEntity<String> responseEntity = restTemplate.postForEntity(batchUrl, entity, String.class);
        Object returnInfo = responseEntity.getBody();
        return JSON.parseObject(returnInfo.toString(), WashReturn.class);
    }

    public List<HaoHanUploadBarCodeVO> queryTiaoMa(String data) {
        RestTemplate restTemplate = new RestTemplate();
        String url = queryTiaoMaUrl + "?Tiaoma=" + data;

        // 设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.set("Authorization", "appCode " + appCode);

        // 创建 HttpEntity，将 headers 传入
        HttpEntity<String> entity = new HttpEntity<>(headers);

        // 发送请求，使用 exchange 方法
        ResponseEntity<List<HaoHanUploadBarCodeVO>> responseEntity = restTemplate.exchange(
                url,
                HttpMethod.GET,
                entity,
                new ParameterizedTypeReference<List<HaoHanUploadBarCodeVO>>() {
                }
        );

        return responseEntity.getBody();
    }

}
